from rest_framework import serializers

from goods.models import SKU
from goods.search_indexes import SKUIndex
from drf_haystack.serializers import HaystackSerializer


class GoodsSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = "__all__"


class SKUSerializer(serializers.ModelSerializer):
    """
    SKU序列化器
    """

    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'default_image_url', 'comments')


class SKUIndexSerializer(HaystackSerializer):
    """
    SKU索引结果数据序列化器
    检查前端传入的参数text，并且检索出数据后再使用这个序列化器返回给前端
    """
    object = SKUSerializer(read_only=True)  # 向前端返回数据时序列化的字段

    class Meta:
        index_classes = [SKUIndex]
        fields = ('text', 'object')
